//
//  DiceRollerView.swift
//  AppleDemo
//
//  Created by Just 花语 on 2025/10/24.
//

import SwiftUI


struct DiceRollerView: View {
    @State private var numberOfDice: Int = 1
    
    var body: some View {
        VStack{
            Text("Dice Roller")
                .font(.largeTitle.lowercaseSmallCaps())
            HStack{
                ForEach(1...numberOfDice,id:\.description) { _ in
                    DiceView()
                }
            }
            HStack{
                Button{
                    withAnimation {
                        numberOfDice -= 1
                    }
                }label:{
                    Label("Remove Die",systemImage: "minus.circle")
                        .font(.system(size:30))
                        .symbolRenderingMode(.palette)
                        .foregroundStyle(
                            numberOfDice <= 1 ? .gray : .red,
                            numberOfDice <= 1 ? .gray : .orange
                        )
                    
                }
                .disabled(numberOfDice <= 1)
                Button("Add Die",systemImage: "plus.circle"){
                    withAnimation {
                        numberOfDice += 1
                    }
                }.disabled(numberOfDice >= 6)
                    .font(.system(size: 30))
                    .foregroundStyle(
                        numberOfDice >= 6 ? .gray : .red,
                        numberOfDice >= 6 ? .gray : .orange
                    )
            }.labelStyle(.iconOnly)
                .padding()
            
        }
    }
}

struct DiceView:View {
    @State var roll: Int = 1;
    var body: some View {
        VStack {
            Image(systemName: "die.face.\(roll).fill")
                .resizable()
                .frame(maxWidth: 100, maxHeight: 100)
                .aspectRatio(1,contentMode: .fit)
                .onTapGesture {
                    withAnimation {
                        roll = Int.random(in: 1...6)
                    }
                }
//            Button("Roll"){
//                withAnimation {
//                    roll = Int.random(in: 1...6)
//                }
//            }
            .buttonStyle(.bordered)
            .buttonBorderShape(.capsule)
        }
    }
}

#Preview {
    DiceRollerView()
}


